package com.tele.service.imps;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.tele.domain.bo.ChMenuBo;
import com.tele.domain.vo.ChMenuVo;
import com.tele.domain.ChMenu;
import com.tele.mapper.ChMenuMapper;
import com.tele.service.ints.IChMenuService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-21
 */
@RequiredArgsConstructor
@Service
public class ChMenuServiceImpl implements IChMenuService {

    private final ChMenuMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public ChMenuVo queryById(Long menuId){
        return baseMapper.selectVoById(menuId);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<ChMenuVo> queryPageList(ChMenuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ChMenu> lqw = buildQueryWrapper(bo);
        Page<ChMenuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<ChMenuVo> queryList(ChMenuBo bo) {
        LambdaQueryWrapper<ChMenu> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ChMenu> buildQueryWrapper(ChMenuBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ChMenu> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMenuName()), ChMenu::getMenuName, bo.getMenuName());
        lqw.eq(bo.getParentId() != null, ChMenu::getParentId, bo.getParentId());
        lqw.eq(bo.getOrderNum() != null, ChMenu::getOrderNum, bo.getOrderNum());
        lqw.eq(StringUtils.isNotBlank(bo.getPath()), ChMenu::getPath, bo.getPath());
        lqw.eq(StringUtils.isNotBlank(bo.getComponent()), ChMenu::getComponent, bo.getComponent());
        lqw.eq(StringUtils.isNotBlank(bo.getQueryParam()), ChMenu::getQueryParam, bo.getQueryParam());
        lqw.eq(bo.getIsFrame() != null, ChMenu::getIsFrame, bo.getIsFrame());
        lqw.eq(bo.getIsCache() != null, ChMenu::getIsCache, bo.getIsCache());
        lqw.eq(StringUtils.isNotBlank(bo.getMenuType()), ChMenu::getMenuType, bo.getMenuType());
        lqw.eq(StringUtils.isNotBlank(bo.getVisible()), ChMenu::getVisible, bo.getVisible());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ChMenu::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPerms()), ChMenu::getPerms, bo.getPerms());
        lqw.eq(StringUtils.isNotBlank(bo.getIcon()), ChMenu::getIcon, bo.getIcon());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(ChMenuBo bo) {
        ChMenu add = BeanUtil.toBean(bo, ChMenu.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMenuId(add.getMenuId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(ChMenuBo bo) {
        ChMenu update = BeanUtil.toBean(bo, ChMenu.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ChMenu entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
